//
//  ALPListenListDetailsVC.m
//  AlpFM
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPListenListDetailsVC.h"
#import "ALPAppTools.h"
#import "ALPListenListDetailsModel.h"
#import "ALPgatherLable.h"
#import "ALPListenListDetailsCell.h"
#import "ALPPlayViewController.h"
#import "ALPEditorPlayVC.h"

@interface ALPListenListDetailsVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UILabel *bigLable;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) ALPListenListDetailsModel *mol;

@end

@implementation ALPListenListDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIImage *image1 = [UIImage imageNamed:@"fanhuijiantou1"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(fanhui:)];
    self.navigationItem.title = @"听单详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 78;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mol.list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ALPListenListDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.mol = [self.mol.list objectAtIndex:indexPath.row];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];  //取消选中状态
    if (self.contentType == 1) {
        ALPEditorPlayVC *VC = [[ALPEditorPlayVC alloc]init];
        ALPListenListDetailModelList *mol = [self.mol.list objectAtIndex:indexPath.row];
        VC.indice = indexPath.row + 1;
        VC.idid = mol.tracksCounts;
        VC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:VC animated:YES completion:^{
            
            
        }];
    } else {
        ALPPlayViewController *VC  = [[ALPPlayViewController alloc]init];
        ALPListenListDetailModelList *mol = [self.mol.list objectAtIndex:indexPath.row];
        VC.song_ID = mol.playsCounts;
        [self presentViewController:VC animated:YES completion:^{
            
            
        }];
    }
    


}


-(void)getData{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%ld&position=1&title=精品听单",self.specialId];
    [ALPAppTools getWithAFNurl:str block:^(id result) {
        self.mol = [[ALPListenListDetailsModel alloc]init];
        [self.mol setValuesForKeysWithDictionary:result];
        [self.myTableView reloadData];
    
        //tableView
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375,553) style:UITableViewStylePlain];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
        self.myTableView.bounces = NO;
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消分割线
        [self.view addSubview:self.myTableView];
        [self creatHeadView];
        [self.myTableView registerClass:[ALPListenListDetailsCell class] forCellReuseIdentifier:@"cell"];
        
    } error:^(NSError *error) {
        
        
    }];
    
}

-(void)creatHeadView {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, self.mol.smallModel.intro.length + 250)];
    //图片
    UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 45, 45)];
    myImage.image = [UIImage imageNamed:@"shu"];
    [view addSubview:myImage];
    
    UIImageView *zuo = [[UIImageView alloc]initWithFrame:CGRectMake(130, 103, 15, 15)];
    zuo.image = [UIImage imageNamed:@"lingxing"];
    [view addSubview:zuo];
    
    UIImageView *you = [[UIImageView alloc]initWithFrame:CGRectMake(200, 103, 15, 15)];
    you.image = [UIImage imageNamed:@"lingxing"];
    [view addSubview:you];
    
    //简介
    [ALPgatherLable creatLable:160 y:100 width:30 height:20 wordsize:14 textcolor:[UIColor grayColor] backColor:nil text:@"简介" block:^(UILabel *label) {
        [view addSubview:label];
    }];
    
    //内容
    [ALPgatherLable creatLable:30 y:120 width:300 height:self.mol.smallModel.intro.length + 40 wordsize:14 textcolor:[UIColor grayColor] backColor:[UIColor whiteColor] text:self.mol.smallModel.intro block:^(UILabel *label) {
        label.numberOfLines = 0;
        [view addSubview:label];
        
    }];
    
    //标题
    [ALPgatherLable creatLable:65 y:35 width:280 height:60 wordsize:20 textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] text:self.mol.smallModel.title block:^(UILabel *label) {
        label.numberOfLines = 0;
        [view addSubview:label];
        
    }];
    
    
    //根据字符串内容(中,英)求出字节长度  一个汉字 = 2个字节
    NSInteger strlength = 0;
    char* p = (char*)[self.mol.smallModel.nickname cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[self.mol.smallModel.nickname lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
        
    }
    //        NSLog(@"%ld",strlength);
    
    //小编
    [ALPgatherLable creatLable:375 - strlength * 7 - 40 - 15 y:self.mol.smallModel.intro.length + 170 width:40 height:30 wordsize:13 textcolor:[UIColor grayColor] backColor:[UIColor whiteColor] text:@"小编:" block:^(UILabel *label) {
        [view addSubview:label];
        
    }];
    
    //小编name
    [ALPgatherLable creatLable:375 - strlength * 7 y:self.mol.smallModel.intro.length + 170 width:strlength * 7 height:30 wordsize:13 textcolor:[UIColor grayColor] backColor:[UIColor whiteColor] text:self.mol.smallModel.nickname block:^(UILabel *label) {
        [view addSubview:label];
        
    }];
    
    //小编头像
    UIImageView *touxiang = [[UIImageView alloc]initWithFrame:CGRectMake(355 - strlength * 7, self.mol.smallModel.intro.length + 177, 15, 15)];
    [touxiang sd_setImageWithURL:[NSURL URLWithString:self.mol.smallModel.smallLogo] placeholderImage:nil];
    [view addSubview:touxiang];
    
    UIImageView *ditu = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.mol.smallModel.intro.length + 170 + 10 + 20, 375, 10)];
    ditu.image = [UIImage imageNamed:@"ditu1"];
    [view addSubview:ditu];
    
    //三角
    UIImageView *sanjiao = [[UIImageView alloc]initWithFrame:CGRectMake(10,self.mol.smallModel.intro.length + 170 + 10 + 10 + 20 + 10, 16, 16)];
    sanjiao.image = [UIImage imageNamed:@"sanjiao"];
    [view addSubview:sanjiao];
    
    //听单列表
    [ALPgatherLable creatLable:30 y:self.mol.smallModel.intro.length + 170 + 10 + 10 + 20 + 10 width:100 height:20 wordsize:13 textcolor:[UIColor grayColor] backColor:nil text:@"听单列表" block:^(UILabel *label) {
        [view addSubview:label];
    }];
    
    self.myTableView.tableHeaderView = view;
}

-(void)fanhui:(UIBarButtonItem *)btn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
